<?php
namespace Home\Controller;
use Think\Controller;
//set_time_limit(100);


class FlashsalesController extends BaseController {

    public function __construct()
    {
        parent::__construct();
        $this->local_id = I('local_id', '', 'intval') ?: session('home_local_id');
    }

    public function index(){

    }

    /**
     * $type：0-初始化
     */
    public function main(){

        $time = time();
        $mindate = date('Y-m-d');
        $maxdate = date('Y-m-d',$time+604800);
        $data = M('starbuy_flashsales sf')->field('sf.special_id,sf.flashsales_date')
            ->join(C('DB_PREFIX').'starbuy_flashsales_time sft on sft.special_id = sf.special_id', 'inner')
            ->order('flashsales_date ASC')
            ->where(['sf.local_id'=>$this->local_id,'sf.is_start'=>'1','flashsales_date'=>[['egt',$mindate],['lt',$maxdate]]])
            ->group('sf.special_id')
            ->select();
        //dump($data);
        /*$time_data = M('starbuy_flashsales_time')->order('begin_time ASC')->where(['special_id' => $data[0]['special_id']])->select();
        //dump($time_data);
        */
        //查询图片
        $images = M('image_image_attach a')->join('left join sdb_image_image b on a.image_id = b.image_id')
            ->where(['a.target_id' => $data[0]['special_id'],'a.target_type' => 'FlashSales'])
            ->select();
        //dump($images);
        //$this->assign('time_data',$time_data);
        $this->assign('images',$images);
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 闪购
     */
    public function timeProduct(){
        $special_id = I('special_id',0) or exit(json_encode(['status'=> false,'msg'=>'参数错误！']));
        //dump($special_id);die;
        //闪购图片
        $starbuyImages = M('image_image_attach a')->join('left join sdb_image_image b on a.image_id = b.image_id')
            ->where(['a.target_id' => $special_id,'a.target_type' => 'FlashSales'])
            ->select();
        //时间段
        $time_data = M('starbuy_flashsales_time')->order('begin_time ASC')->where(['special_id'=>$special_id])->select();
        if(!$time_data){
            exit(json_encode(['status'=> false,'msg'=>'更新失败！']));
        }
        $begin_tid = 0;
        //第当前时间段的商品
        $time = time();
        $day = date("Y-m-d",$time);
        //比较是否是当天的闪购
        $flashsales_date = M('starbuy_flashsales')->where(['special_id'=>$special_id])->find()['flashsales_date'];
        if($day == $flashsales_date){
            foreach($time_data as $k1 => &$v1){
                if($time >= $v1['begin_time'] && $time < $v1['end_time']){
                    $begin_tid = $v1['tid'];
                    $v1['default'] = true;
                    break;
                }else{
                    $begin_tid = $v1['tid'];
                }
            }
        }else{
            $begin_tid = $time_data[0]['tid'];
        }

        //dump($begin_tid);
        $product_data = $this->flashsalesProducts($begin_tid);
        if(!$product_data){
            exit(json_encode(['status'=> false,'msg'=>'更新失败！']));
        }
        $list = ['time_data'=>$time_data,'product_data'=>$product_data['list'],'image'=>$product_data['image'],'starbuyImages'=>$starbuyImages];
        exit(json_encode(['status'=> true,'msg'=>'更新成功！','list'=>$list]));
    }

    /**
     * 闪购时间段对应的商品
     * @param $tid
     * @param int $type 0-方法、1-接口
     * @return mixed
     */
    public function flashsalesProducts($tid = 0,$type = 0){

        if($type){
            $tid = I('get.tid',0) or exit(json_encode(['status'=>false,'msg' =>'参数错误']));
        }else{
            if(!$tid){
                return false;
            }
        }
        $data = M('starbuy_flashsales_goods')->alias('sfg')
            ->field('sfg.special_id,sfg.tid,sfg.promotion_price,sfg.ini_y_tnum,sfg.initnum,sfg.repertory_num,sfg.countnum,sp.*')
            ->join(C('DB_PREFIX').'b2c_store_product sp on sp.store_pid = sfg.store_pid', 'LEFT')
            ->join(C('DB_PREFIX').'starbuy_flashsales sf on sf.special_id = sfg.special_id', 'LEFT')
            ->where(['tid'=>$tid,'sf.is_start'=>'1'])
            ->select();
        //dump($data);die;
        if($data){
            $ids = [];
            foreach($data as $k1 => $v1){
                $ids[] = $v1['goods_id'];
                $data[$k1]['url'] = url('Goods/detail',['id'=>$v1['store_pid']]);
            }
            $image = M('b2c_goods a')->join(C('DB_PREFIX').'image_image b on a.image_default_id=b.image_id', 'LEFT')->where(['a.goods_id'=>['in',$ids]])
                ->getField('a.goods_id,l_url');
        }

        if($type){
            exit(json_encode($data ? ['status'=>true,'msg' =>'成功','list'=>$data,'image'=>$image] : ['status'=>false,'msg' =>'失败']));
        }else{
            return ['list'=>$data,'image'=>$image];
        }
    }


  

    

   

}